Typical HAL SPI use cases for migration ¶
This section provides information about typical HAL SPI use cases for migration.
Note
The processes for transmitting and receiving data using polling, interrupt, or DMA are the same in HAL1 and HAL2.
Use case: Full duplex transmission/reception in polling mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_SEND 256
#define DATA_TO_RECEIVE DATA_TO_SEND
SPI_HandleTypeDef hspi1;
uint8_t aTxBuffer[] = "*** SPI communication based on Polling mode***"
uint8_t aRxBuffer[BUFFERSIZE]
HAL_SPI_Transmit(&hspi1, aTxBuffer, DATA_TO_SEND, 500);
HAL_SPI_Receive(&hspi1, aRxBuffer, DATA_TO_RECEIVE, 500);
HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)aTxBuffer, (uint8_t *)aRxBuffer, BUFFERSIZE, 500);
|
#define DATA_TO_SEND 256
#define DATA_TO_RECEIVE DATA_TO_SEND
hal_spi_handle_t hspi1;
hal_spi_config_t spi_config;
uint8_t aTxBuffer[] = "*** SPI communication based on Polling mode***"
uint8_t aRxBuffer[BUFFERSIZE]
HAL_SPI_Transmit(&hspi1, (const void *)pTxData, DATA_TO_SEND, 500);
HAL_SPI_Receive(&hspi1,( void *)aRxBuffer, DATA_TO_RECEIVE, 500);
HAL_SPI_TransmitReceive(pSPI, pTxData, &RxBuffer, BUFFER_SIZE, 500);
|
Key changes ¶
HAL2 uses
hal_spi_handle_tinstead ofSPI_HandleTypeDef.
Use case: SPI receive in interrupt mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_RECEIVE_SIZE 255
uint8_t aRxBuffer[BUFFERSIZE];
SPI_HandleTypeDef hspi1;
HAL_SPI_RegisterCallback(&hspi1, HAL_SPI_RX_CB_ID, USER_SPI_RxCpltCallback);
HAL_SPI_Receive_IT(&hspi1, aRxBuffer, DATA_TO_RECEIVE_SIZE);
|
#define DATA_TO_RECEIVE_SIZE 255
uint8_t aRxBuffer[BUFFERSIZE];
hal_spi_handle_t hspi1;
HAL_SPI_RegisterRxCpltCallback(&hspi1, USER_SPI_RxCpltCallback);
HAL_SPI_Receive_IT(&hspi1, aRxBuffer, DATA_TO_RECEIVE_SIZE);
|
Key changes ¶
RX complete callback registration changes from
HAL_SPI_RegisterCallback(..., HAL_SPI_RX_CB_ID, ...)toHAL_SPI_RegisterRxCpltCallback().HAL2 uses
hal_spi_handle_tinstead ofSPI_HandleTypeDef.
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
example_hal_spi_full_duplex_two_boards_com_it_responder |
Data buffer transmission/reception between two boards via SPI using Interrupt mode |
Use case: SPI transmit in interrupt mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_SEND 256
SPI_HandleTypeDef hspi1;
uint8_t aTxBuffer[] = "*** SPI communication based on IT mode ***"
HAL_SPI_RegisterCallback(&hspi1, HAL_SPI_TX_CB_ID, USER_SPI_TxCpltCallback);
HAL_SPI_Transmit_IT(&hspi1, aTxBuffer, DATA_TO_SEND);
|
#define DATA_TO_SEND 256
hal_spi_handle_t hspi1;
uint8_t aTxBuffer[] = "*** SPI communication based on IT mode ***"
HAL_SPI_RegisterTxCpltCallback(&hspi1, USER_SPI_TxCpltCallback);
HAL_SPI_Transmit_IT(&hspi1, aTxBuffer, DATA_TO_SEND);
|
Key changes ¶
TX complete callback registration changes from
HAL_SPI_RegisterCallback(..., HAL_SPI_TX_CB_ID, ...)toHAL_SPI_RegisterTxCpltCallback().HAL2 uses
hal_spi_handle_tinstead ofSPI_HandleTypeDef.
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
example_hal_spi_full_duplex_two_boards_com_it_controller |
How to handle data buffer transmission/reception between two boards via SPI using Interrupt mode. |
Use case: SPI transmit/receive in interrupt mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_SEND 256
#define DATA_TO_RECEIVE DATA_TO_SEND
SPI_HandleTypeDef hspi1;
uint8_t aTxBuffer[] = "*** SPI communication based on IT mode***"
uint8_t aRxBuffer[DATA_TO_RECEIVE]
HAL_SPI_RegisterCallback(&hspi1, HAL_SPI_TX_RX_COMPLETE_CB_ID, USER_SPI_TxRxCpltCallback);
HAL_SPI_TransmitReceive_IT(&hspi1, pTxData, &RxBuffer, DATA_TO_SEND);
|
#define DATA_TO_SEND 256
#define DATA_TO_RECEIVE DATA_TO_SEND
hal_spi_handle_t hspi1;
uint8_t aTxBuffer[] = "*** SPI communication based on IT mode***"
uint8_t aRxBuffer[DATA_TO_RECEIVE]
HAL_SPI_RegisterTxRxCpltCallback(&hspi1, USER_SPI_TxRxCpltCallback);
HAL_SPI_TransmitReceive_IT(&hspi1, aTxBuffer, aRxBuffer, DATA_TO_RECEIVE);
|
Key changes ¶
TX/RX complete callback registration changes from
HAL_SPI_RegisterCallback(..., HAL_SPI_TX_RX_COMPLETE_CB_ID, ...)toHAL_SPI_RegisterTxRxCpltCallback().HAL2 uses
hal_spi_handle_tinstead ofSPI_HandleTypeDef.
Use case: SPI transmit in DMA mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_SEND 256
#define DATA_TO_RECEIVE DATA_TO_SEND
SPI_HandleTypeDef hspi1;
uint8_t aTxBuffer[] = "*** SPI communication based on DMA mode ***"
HAL_SPI_RegisterCallback(&hspi1, HAL_SPI_TX_COMPLETE_CB_ID, USER_SPI_TxCpltCallback);
HAL_SPI_Transmit_DMA(&hspi1, aTxBuffer, DATA_TO_SEND);
|
#define DATA_TO_SEND 256
#define DATA_TO_RECEIVE DATA_TO_SEND
hal_spi_handle_t hspi1;
uint8_t aTxBuffer[] = "*** SPI communication based on DMA mode ***"
HAL_SPI_RegisterTxCpltCallback(&hspi1, USER_SPI_TxCpltCallback);
HAL_SPI_Transmit_DMA(&hspi1, aTxBuffer, DATA_TO_SEND);
|
Key changes ¶
TX complete callback registration changes from
HAL_SPI_RegisterCallback(..., HAL_SPI_TX_COMPLETE_CB_ID, ...)toHAL_SPI_RegisterTxCpltCallback().HAL2 uses
hal_spi_handle_tinstead ofSPI_HandleTypeDef.
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
example_hal_spi_full_duplex_two_boards_com_dma_controller |
How to handle data buffer transmission/reception between two boards via SPI using DMA mode. |
Use case: SPI receive in DMA mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_RECEIVE_SIZE 255
uint8_t aRxBuffer[BUFFERSIZE];
SPI_HandleTypeDef hspi1;
HAL_SPI_RegisterCallback(&hspi1, HAL_SPI_RX_COMPLETE_CB_ID, USER_SPI_RxCpltCallback);
HAL_SPI_Receive_DMA(&hspi1, aRxBuffer, DATA_TO_RECEIVE_SIZE);
|
#define DATA_TO_RECEIVE_SIZE 255
uint8_t aRxBuffer[BUFFERSIZE];
hal_spi_handle_t hspi1;
HAL_SPI_RegisterRxCpltCallback(&hspi1, USER_SPI_RxCpltCallback);
HAL_SPI_Receive_DMA(&hspi1, aRxBuffer, DATA_TO_RECEIVE_SIZE);
|
Key changes ¶
RX complete callback registration changes from
HAL_SPI_RegisterCallback(..., HAL_SPI_RX_COMPLETE_CB_ID, ...)toHAL_SPI_RegisterRxCpltCallback().HAL2 uses
hal_spi_handle_tinstead ofSPI_HandleTypeDef.
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
example_hal_spi_full_duplex_two_boards_com_it_responder |
How to handle data buffer transmission/reception between two boards via SPI using DMA mode. |
Use case: SPI transmit/receive in DMA mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_SEND 255
#define DATA_TO_RECEIVE_SIZE DATA_TO_SEND
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];
uint8_t aTxBuffer[] = "*** SPI communication based on DMA mode ***"
SPI_HandleTypeDef hspi1;
HAL_SPI_RegisterCallback(&hspi1, HAL_SPI_RX_COMPLETE_CB_ID, USER_SPI_TxRxCpltCallback);
HAL_SPI_TransmitReceive_DMA(&hspi1, aTxBuffer, aRxBuffer, DATA_TO_RECEIVE_SIZE);
|
#define DATA_TO_SEND 255
#define DATA_TO_RECEIVE_SIZE DATA_TO_SEND
uint8_t aRxBuffer[BUFFERSIZE];
uint8_t aTxBuffer[] = "*** SPI communication based on DMA mode ***"
hal_spi_handle_t hspi1;
HAL_SPI_RegisterTxRxCpltCallback(&hspi1, HAL_SPI_RX_COMPLETE_CB_ID, USER_SPI_TxRxCpltCallback);
HAL_SPI_TransmitReceive_DMA(&hspi1, aTxBuffer, aRxBuffer, DATA_TO_RECEIVE_SIZE);
|
Key changes ¶
TX/RX complete callback registration uses the dedicated HAL2 registration API (
HAL_SPI_RegisterTxRxCpltCallback()).HAL2 uses
hal_spi_handle_tinstead ofSPI_HandleTypeDef.













